package com.example.daxiguaforharmony;

import com.example.daxiguaforharmony.utils.DbKvManager;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.content.Intent;
//import ohos.miscservices.timeutility.Timer;
import ohos.agp.components.ComponentProvider;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.Timer;
import java.util.TimerTask;

public class GameServiceAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "GameServiceAbility");
    private Long formId;
    DbKvManager dbKvManager;

    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "GameServiceAbility::onStart");
        super.onStart(intent);

        dbKvManager = new DbKvManager(this.getContext());
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "GameServiceAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        HiLog.info(LABEL_LOG, "GameServiceAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        System.out.println("-----------尝试读取数据库onCommand===----------");
        formId = intent.getLongParam("formId",0);
        System.out.println("onCommand===formId"+formId);
        startTimer();
    }

    // 定时器，每秒执行一次
    private void startTimer() {
        Timer timer = new Timer();
        long formId_value = formId;
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        updateForms(formId_value);
                    }
                },
                0,
                1000L*60);//1min
    }

    //更新卡片方法
    private void updateForms(long formId_value) {
        System.out.println("-----------刷新卡片===----------"+formId_value);
//        if (picNum==1){
//            picNum = 2;
//        }else{
//            picNum = 1;
//        }
//        ZSONObject params = ZSONObject.stringToZSON("{\"itemContent\":\"定时换图启动中ing\",\"imageIndex\":\"\\/common\\/test"+picNum+".jpg\"}");
//        FormBindingData bindingData = new FormBindingData(params);
        if(dbKvManager == null)
        {
            dbKvManager = new DbKvManager(this.getContext());
        }

        String score1 = dbKvManager.read("score");
        score1 = score1.equals("") ?"0":score1;
        System.out.println("startTimer onUpdateForm 得分2======="+score1);
        String score_high = dbKvManager.read("score_high");
        System.out.println("startTimer onUpdateForm 最高得分1======="+score_high);
        //score_high = String.valueOf(score_high);
        score_high = score_high.equals("") || score_high.equals("0") ?score1:score_high;
        System.out.println("startTimer onUpdateForm 最高得分2======="+score_high);
        //String formData = "1289";
        int layoutId = ResourceTable.Layout_form_image_with_information_widget_2_2;

        ComponentProvider componentProvider = new ComponentProvider(layoutId, this.getContext());
        componentProvider.setText(ResourceTable.Id_text_title, score_high);

        // 从数据库中获取卡片信息
        // updateForm(formId_value,bindingData);
        try {
            updateForm(formId_value,componentProvider);
        } catch (FormException e) {
            e.printStackTrace();
        }
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
    }
}